package com.tanhua.dubbo.api.mongo;

import com.tanhua.domain.mongo.*;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;
@Service
 //此类用于查询问卷列表
public class SoulServiceApiImpl implements SoulServiceApi {
    @Autowired
   private  MongoTemplate mongoTemplate;
    @Override
    public LHOptions findOptionByOptionId(String optionId) {
        return mongoTemplate.findById(new ObjectId(optionId),LHOptions.class);
    }

    @Override
    public String addResult(LHResult lhResult) {
        ObjectId id = new ObjectId();
        lhResult.setId(id);

        mongoTemplate.insert(lhResult);

        return id.toHexString();
    }

    @Override
    public LHQuestion findQuestionnaireByQuestionId(String questionId) {

        return mongoTemplate.findById(new ObjectId(questionId), LHQuestion.class);
    }

    /** 此方法用于根据问题的id查询问卷的id
     * @param id 问题的id
     * @return 问卷的id
     */
    @Override
    public ObjectId findQuestionnaireId(ObjectId id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(id));
        LHQuestion one = mongoTemplate.findOne(query, LHQuestion.class);
        ObjectId id1 = one.getId();
        return id1;
    }

    /**此方法用于查询最新的报告
     * @param userId 用户的id
     * @return
     */
    @Override
    public boolean findReportId(Long userId) {
        System.out.println("userID是"+userId);
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId+""));
        long count = mongoTemplate.count(query, LHResult.class);
        if (count>0){
            return true;
        }
        return false;
    }

    /**此方法用于查询用户的灵魂测试有没有锁
     * @param userId 用户的id
     * @return true代表锁上了，false代表没有锁上
     */
    @Override
    public boolean findLock(Long userId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId));
        long count = mongoTemplate.count(query, LHlock.class);
        if (count>0){
            return  true;
        }
        return false;
    }

    /**此方法用于给用户的灵魂测试加锁，用户的灵魂测试没有锁就加上，有就不加
     * @param userId 用户的id
     */
    @Override
    public void addLock(Long userId) {
        boolean lock = findLock(userId);
        if (lock){
            return;
        }
        LHlock k = new LHlock();
        for (int i = 1; i <4 ; i++) {
            k.setUserId(userId);
            k.setLevel(i);
            k.setLock(1);
            if (i==1){
                k.setLock(0);
            }
           mongoTemplate.insert(k);
        }
    }

    @Override
    public LHOptions findOneOption(String id) {
        //此方法用于查询一个选项的所有信息
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(new ObjectId(id)));
      LHOptions one = mongoTemplate.findOne(query, LHOptions.class);
        return one;
    }

    /**此方法用于查询问卷
     * @param userId 用户的id
     * @param level 问卷的级别
     * @return Integer 0表示没有上锁，1表示上锁了
     */
    @Override
    public LHlock findIsLock(Long userId, Integer level) {
        Query query = new Query();
        query.addCriteria(Criteria.where("UserId").is(userId).and("level").is(level));
        LHlock one = mongoTemplate.findOne(query, LHlock.class);
        return one;
    }

    /**此方法用于查询问卷列表
      * @return 问卷列表
      */
    @Override
    public List<LHQuestionnaire> findTest() {
        Query query = new Query();
        List<LHQuestionnaire> list = mongoTemplate.find(query, LHQuestionnaire.class);
        return list;
    }

    @Override
    public List<LHQuestion> findQuestion(String id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("questionnaireId").is(new ObjectId(id)));
        List<LHQuestion> lhOptions = mongoTemplate.find(query, LHQuestion.class);
        return lhOptions;
    }

    @Override
    public List<LHOptions> findOption(String id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("questionId").is(new ObjectId(id)));
        List<LHOptions> lhOptions = mongoTemplate.find(query, LHOptions.class);
        return lhOptions;
    }

}
